﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace LanAnh
{
    public partial class frmDoiMatKhau : Office2007Form 
    {
        public frmDoiMatKhau(string ten)
        {            
            InitializeComponent();
            TenDangNhap = ten;
        }
        Data data = new Data();
        DataTable dt;
        string sql;
        string TenDangNhap;
        string matkhaucu;
        private void buttonX1_Click(object sender, EventArgs e)
        {
            
            
            if (txtPassMoi.Text != txtRePassMoi.Text)
            {
                lbThongBao.Visible = true;
                lbThongBao.Text = "Hai Mật khẩu không trùng khớp";
            }
            else
            {
                sql = "SELECT * FROM tblDangNhap WHERE cTenDangNhap=N'" + TenDangNhap + "'";
                dt = data.executeQueryTable(sql);
                matkhaucu = dt.Rows[0]["cMatKhau"].ToString();
                if (getMd5Hash(txtPassCu.Text) == matkhaucu)
                {
                    sql = "UPDATE tblDangNhap SET cMatKhau=N'" + getMd5Hash(txtPassMoi.Text) + "' WHERE cTenDangNhap=N'"+TenDangNhap +"'";
                    data.executeNonQuery(sql);
                    this.Close();
                }
                else
                {
                    lbThongBao.Visible = true;
                    lbThongBao.Text = "Mật khẩu cũ không đúng";
                    txtPassCu.Focus();
                }
            }

        }
        private string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }  
        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtRePassMoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonX1_Click(sender, e);
            }
        }

        
    }
}
